package com.fanco.learn_datastruct_leetcode.juc;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class ThreadA extends Thread {

    @Override
    public void run () {
        System.out.println(Thread.currentThread().getName() + "\t 通过 继承Thread实现");
    }

}

class ThreadB implements Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "\t 通过Runable创建");
    }
}

class ThreadC implements Callable {

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "\t 通过Callable创建");
        return "test";
    }
}
/**
 * @author fanco
 * @Desc Java实现线程的几种方式：
 * 1. 继承Thread 类
 * 2. 实现Runable接口
 * 3. 实现Callable接口 + FutureTask
 * 4. 通过线程池 // 暂不做演示
 */
public class CallableDemo {
    public static void main(String[] args) {

        ThreadA threadA = new ThreadA();
        Thread a = new Thread(threadA, "甲");
        a.start();

        ThreadB threadB = new ThreadB();
        Thread b = new Thread(threadB, "乙");
        b.start();

        ThreadC threadC = new ThreadC();
        FutureTask futureTask = new FutureTask(threadC);
        Thread c = new Thread(futureTask, "丙");
        c.start();

        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}
